package com.shop.cereshop.agent.service.rank.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.domain.rank.AgentRank;
import com.shop.cereshop.agent.dao.rank.AgentRankDAO;
import com.shop.cereshop.agent.dao.referral.AgentReferralDAO;
import com.shop.cereshop.agent.page.rank.RankItem;
import com.shop.cereshop.agent.page.rank.RankVO;
import com.shop.cereshop.agent.param.rank.AgentRankDTO;
import com.shop.cereshop.agent.service.rank.AgentRankService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lx
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AgentRankServiceImpl extends ServiceImpl<AgentRankDAO, AgentRank> implements AgentRankService {

  private final AgentRankDAO agentRankDAO;

  private final AgentReferralDAO agentReferralDAO;

  @Override
  public RankVO getRank(AgentRankDTO param) {
    //根据不同类型查询不同排行榜数据
    switch (param.getRankType()) {
      case 1:
        //商家数量
        return getShopNum(param);
      case 2:
        //商家成交额
        return getShopAmount(param);
      case 3:
        //用户数量
        return getUserNum(param);
      case 4:
        //用户成交额
        return getUserAmount(param);
      case 5:
        //业务员数量
        return getSalespersonNum(param);
      case 6:
        //代理商数量
        return getAgentNum(param);
      default:
        return null;
    }
  }

  @Override
  public void applyDayRank(AgentRank param) {

  }

  public RankVO getAgentNum(AgentRankDTO param) {
    //创建分页参数
    PageHelper.startPage(param.getPage(), param.getPageSize());
    //查询代理商数量
    List<RankItem> rankList = agentReferralDAO.getAgentNum();
    RankVO rankVO = new RankVO();
    PageInfo pageInfo = new PageInfo(rankList);
    Page<RankItem> page = new Page<>(pageInfo.getList(), pageInfo.getTotal());
    rankVO.setRankPage(page);
    //查询我的数据
    RankItem rankItem = agentReferralDAO.getMyAgentNum(param);
    rankVO.setMyRank(rankItem);
    return rankVO;
  }

  public RankVO getSalespersonNum(AgentRankDTO param) {
    //创建分页
    PageHelper.startPage(param.getPage(), param.getPageSize());
    //查询业务员数量
    List<RankItem> rankList = agentReferralDAO.getSalespersonNum();
    RankVO rankVO = new RankVO();
    PageInfo pageInfo = new PageInfo(rankList);
    Page<RankItem> page = new Page<>(pageInfo.getList(), pageInfo.getTotal());
    rankVO.setRankPage(page);
    //查询我的数据
    RankItem rankItem = agentReferralDAO.getMySalespersonNum(param);
    rankVO.setMyRank(rankItem);
    return rankVO;
  }

  public RankVO getUserAmount(AgentRankDTO param) {
    //创建分页参数
    PageHelper.startPage(param.getPage(), param.getPageSize());
    //查询用户成交额
    List<RankItem> rankList = agentReferralDAO.getUserAmount();
    RankVO rankVO = new RankVO();
    PageInfo pageInfo = new PageInfo(rankList);
    Page<RankItem> page = new Page<>(pageInfo.getList(), pageInfo.getTotal());
    rankVO.setRankPage(page);
    //查询我的数据
    RankItem rankItem = agentReferralDAO.getMyUserAmount(param);
    rankVO.setMyRank(rankItem);
    return rankVO;
  }

  public RankVO getUserNum(AgentRankDTO param) {
    //创建分页参数
    PageHelper.startPage(param.getPage(), param.getPageSize());
    //查询用户数量
    List<RankItem> rankList = agentReferralDAO.getUserNum();
    RankVO rankVO = new RankVO();
    PageInfo pageInfo = new PageInfo(rankList);
    Page<RankItem> page = new Page<>(pageInfo.getList(), pageInfo.getTotal());
    rankVO.setRankPage(page);
    //查询我的数据
    RankItem rankItem = agentReferralDAO.getMyUserNum(param);
    rankVO.setMyRank(rankItem);
    return rankVO;
  }

  public RankVO getShopAmount(AgentRankDTO param) {
    //创建分页参数
    PageHelper.startPage(param.getPage(), param.getPageSize());
    //查询商家成交额
    List<RankItem> rankList = agentReferralDAO.getShopAmount();
    RankVO rankVO = new RankVO();
    PageInfo pageInfo = new PageInfo(rankList);
    Page<RankItem> page = new Page<>(pageInfo.getList(), pageInfo.getTotal());
    rankVO.setRankPage(page);
    //查询我的数据
    RankItem rankItem = agentReferralDAO.getMyShopAmount(param);
    rankVO.setMyRank(rankItem);
    return rankVO;
  }

  public RankVO getShopNum(AgentRankDTO param) {
    //创建分页参数
    PageHelper.startPage(param.getPage(), param.getPageSize());
    //查询商家数量
    List<RankItem> rankList = agentReferralDAO.getShopNum();
    RankVO rankVO = new RankVO();
    PageInfo pageInfo = new PageInfo(rankList);
    Page<RankItem> page = new Page<>(pageInfo.getList(), pageInfo.getTotal());
    rankVO.setRankPage(page);
    //查询我的数据
    RankItem rankItem = agentReferralDAO.getMyShopNum(param);
    rankVO.setMyRank(rankItem);
    return rankVO;
  }
}